package com.tms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.entity.PageBean;
import com.tms.entity.Result;
import com.tms.entity.User;
import com.tms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 存储有关用户信息(User)表控制层
 *
 * @author makejava
 * @since 2024-12-24 23:00:07
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestParam String userName, @RequestParam String password) {
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            return Result.error("用户名或密码不能为空");
        }

        User loginUser = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserName, userName).eq(User::getPassword, password));
        if (loginUser == null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List<User> userList = userService.list();
        return Result.success(userList);
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.error();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.success() : Result.error();
    }

    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id) {
        return userService.removeById(id) ? Result.success() : Result.error();
    }

    //查询（模糊、匹配）
    @GetMapping("/listP")
    public List<User> listP(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getName, user.getName());
        return userService.list(lambdaQueryWrapper);
    }

    @GetMapping("/pageList")
    public Result pageList(@RequestBody PageBean pageBean) {
        HashMap params = pageBean.getParams();

        String userName = params.get("userName").toString();

        Page<User> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        System.out.println(pageBean);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(userName) && !"null".equals(userName)) {
            lambdaQueryWrapper.like(User::getUserName, userName);
        }

        IPage result = userService.pageList(page, lambdaQueryWrapper);

        return Result.success(result.getRecords());
    }


}

